﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Meals
{
    public class MealFood:IMealFood
    {
        public List<Model.Food> GetMealFoods(int MealId, ref Model.NutriLeafDBEntities context)
        {
            List<Model.Food> f = new List<Model.Food>();
         List<Model.FoodInMeal> food= context.FoodInMeals.Where(p => p.MealID == MealId).ToList();
         foreach (Model.FoodInMeal m in food) {
             f.Add(m.Food);
         }
         return f;
        }

        public void AddMealFood(int MealId, int FoodId, ref Model.NutriLeafDBEntities context)
        {
            Model.FoodInMeal food = new Model.FoodInMeal();
            food.MealID = MealId;
            food.FoodID = FoodId;
            context.FoodInMeals.AddObject(food);
            context.SaveChanges();
        }

        public void RemoveFoodFromMeal(int MealId, int FoodId, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.FoodInMeals.Where(p => p.MealID == MealId && p.FoodID==FoodId).First());
            context.SaveChanges();
        }

        public void RemoveFood(int MealFoodId, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.FoodInMeals.Where(p => p.MealID == MealFoodId).First());
            context.SaveChanges();
        }
    }
}